﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using DataModel;
using System.Reflection;
using System.Runtime.Serialization;

namespace CommonFramework
{
    public class Json
    {

        public string GenJsonString()
        {
            Patient patient = new Patient();
            patient.bingliID = "c0d-d-d-d-dd3";

            Patient patient1 = new Patient();
            patient.bingliID = "c0d-d3";

            string result = "";

            try
            {
                result = Newtonsoft.Json.JsonConvert.SerializeObject(patient);
            }
            catch (Exception e)
            {

               
            }
            return result;
        }

        public object GenObject(string json, ref object targetObj)
        {
            if (json != "" || json != null || targetObj != null)
            {
                Patient companies = JsonConvert.DeserializeObject<Patient>(json); //JSON.Deserialize<Dictionary<string, string>[]>(json);
                targetObj = companies;
                //PropertyInfo[] propinfos = propinfos = targetObj.GetType().GetProperties();

                //for (int i = 0; i < propinfos.Length; i++)
                //{
                //    //foreach (var item in companies[0])
                //    {
                //        if (companies.Key == propinfos[i].Name)
                //        {
                //            //如果是nullable类型则需要以其他方法获取类型             
                //            if (propinfos[i].PropertyType.IsGenericType && 
                //                propinfos[i].PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                //            {
                //                //类型转换                  
                //                object ValueA = Convert.ChangeType(item.Value, propinfos[i].PropertyType.GetGenericArguments()[0]);
                //                //自动匹配赋值                            
                //                propinfos[i].SetValue(targetObj, ValueA, null);
                //            }
                //            else
                //            {
                //                object ValueB = Convert.ChangeType(item.Value, propinfos[i].PropertyType);
                //                propinfos[i].SetValue(targetObj, ValueB, null);
                //            } break;
                //        }
                //    }
                //}
                return targetObj;
            }
            return null;
        }

        /// <summary>        
        /// Dictionary<string, string> 转实体自动赋值      
        /// 
        /// </summary>        
        /// <param name="row">JSON转的Dictionary<string, string>[]</param>        
        /// <param name="targetObj">实体object</param>        
        /// <returns>实体object</returns>       
        public object ChangeToObject(string json, object targetObj)
        {
            if (json != "" || json != null || targetObj != null)
            {
                Dictionary<string, string>[] companies = JsonConvert.DeserializeObject<Dictionary<string, string>[]>(json); //JSON.Deserialize<Dictionary<string, string>[]>(json);
                PropertyInfo[] propinfos = propinfos = targetObj.GetType().GetProperties();
                for (int i = 0; i < propinfos.Length; i++)
                {
                    foreach (var item in companies[0])
                    {
                        if (item.Key == propinfos[i].Name)
                        {
                            //如果是nullable类型则需要以其他方法获取类型             
                            if (propinfos[i].PropertyType.IsGenericType && propinfos[i].PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                //类型转换                  
                                object ValueA = Convert.ChangeType(item.Value, propinfos[i].PropertyType.GetGenericArguments()[0]);
                                //自动匹配赋值                            
                                propinfos[i].SetValue(targetObj, ValueA, null);
                            }
                            else
                            {
                                object ValueB = Convert.ChangeType(item.Value, propinfos[i].PropertyType);
                                propinfos[i].SetValue(targetObj, ValueB, null);
                            } break;
                        }
                    }
                }
                return targetObj;
            }
            return null;
        }

    }

}
